using Microsoft.AspNetCore.Mvc;
using ConsoleApi.Services.Interfaces;
using ConsoleApi.Models;

namespace ConsoleApi.Controllers;

/// <summary>
/// 天气测试控制器 - 用于测试OpenWeatherMap集成
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class WeatherTestController : ControllerBase
{
    private readonly IRealWeatherService _weatherService;
    private readonly ILogger<WeatherTestController> _logger;

    public WeatherTestController(IRealWeatherService weatherService, ILogger<WeatherTestController> logger)
    {
        _weatherService = weatherService;
        _logger = logger;
    }

    /// <summary>
    /// 测试获取城市天气
    /// </summary>
    [HttpGet("test/{city}")]
    public async Task<IActionResult> TestWeather(string city)
    {
        try
        {
            _logger.LogInformation("测试获取城市天气: {City}", city);
            
            var weatherData = await _weatherService.GetWeatherByCityAsync(city);
            var alerts = await _weatherService.GetWeatherAlertsAsync(city);
            var airQuality = await _weatherService.GetAirQualityAsync(city);
            
            var result = new
            {
                Success = true,
                City = city,
                WeatherData = weatherData,
                Alerts = alerts,
                AirQuality = airQuality,
                TestTime = DateTime.Now,
                Message = "OpenWeatherMap集成测试成功"
            };
            
            return Ok(result);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "测试获取城市天气失败: {City}", city);
            return BadRequest(new
            {
                Success = false,
                Error = ex.Message,
                TestTime = DateTime.Now,
                Message = "OpenWeatherMap集成测试失败"
            });
        }
    }

    /// <summary>
    /// 获取支持的测试城市列表
    /// </summary>
    [HttpGet("cities")]
    public IActionResult GetTestCities()
    {
        var cities = new[]
        {
            "北京",
            "上海", 
            "广州",
            "深圳",
            "杭州",
            "南京",
            "武汉",
            "成都",
            "重庆",
            "西安",
            "青岛",
            "大连",
            "厦门",
            "苏州",
            "无锡",
            "宁波",
            "长沙",
            "郑州",
            "济南",
            "天津"
        };
        
        return Ok(new
        {
            Success = true,
            Cities = cities,
            Message = "支持测试的城市列表"
        });
    }

    /// <summary>
    /// 测试MCP天气功能
    /// </summary>
    [HttpGet("mcp-test")]
    public IActionResult TestMcpWeather()
    {
        var result = new
        {
            Success = true,
            Message = "MCP天气功能测试",
            Features = new[]
            {
                "实时天气获取",
                "天气预报",
                "天气对生产的影响分析",
                "MCP工具集成",
                "OpenWeatherMap API集成"
            },
            TestTime = DateTime.Now
        };
        
        return Ok(result);
    }
} 